import router from "@/router"
export default {
    namespaced: true,
    state: {
        listTab: [
            {name: "首页", url: '/layout/main', closeable: false, id: 'home'}
        ],
        curTabId: "home"
    },
    getters: {
        curListTab(state) {
            return state.listTab;
        },
        curTabId(state) {
            return state.curTabId;
        }
    },
    mutations: {
        ADD_TAB(state, tab) {
            let curTab = state.listTab.find(item => item.id === tab.id);
            if (curTab) {
                state.curTabId = tab.id;
                return;
            }
            state.curTabId = tab.id;
            state.listTab.push(tab);
            // router.push(tab.url);
        },
        REMOVE_TAB(state, id) {
            let pos = state.listTab.findIndex(item => item.id == id);
            if (pos >= 0) {
                state.listTab.splice(pos, 1);
                if (id == state.curTabId) {
                    let curItem = state.listTab[state.listTab.length - 1];
                    state.curTabId = curItem.id;
                    router.push(curItem.url);
                }
            }
        },
        SET_CUR_TAB(state, id) {
            if (state.curTabId == id) {
                return;
            }
            state.curTabId = id;
            let curItem = state.listTab.find(item => item.id == id);
            router.push(curItem.url);
        },
        CLEAR_MENU(state) {
            state.listTab = [
                {name: "首页", url: '/layout/main', closeable: false, id: 'home'}
            ];
        }
    },
    actions: {
    }
}
